{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73f6211e",
   "metadata": {},
   "source": [
    "* 本周主要内容：图像识别（人脸识别）\n",
    "* 23春_API_人工智能与机器学习_week05\n",
    "* 记录者：赖文佩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b2c9d69",
   "metadata": {},
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43cc871c",
   "metadata": {},
   "source": [
    "> 1.人脸比对            \n",
    "> 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e941f2ce",
   "metadata": {},
   "source": [
    "## 1. 人脸比对实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "71294fa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281819,dd1788e8-83d6-4272-9e94-21de829b255d', 'time_used': 461, 'confidence': 81.858, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'db3b80f0448a5c161f041e50f77b1b96', 'face_rectangle': {'top': 75, 'left': 173, 'width': 114, 'height': 114}}], 'faces2': [{'face_token': 'f95ece6b67f2de4a81df46031256c08e', 'face_rectangle': {'top': 87, 'left': 318, 'width': 131, 'height': 131}}], 'image_id1': 'qesNyjZJhrX0z/ru0Rao5A==', 'image_id2': 'W8NpWno8wrTKB2cFSF1mIg=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "face++人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01 = open('gao01.jpg', 'rb')\n",
    "f_02 = open('gao02.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'image_base64_1':img_01,\n",
    "    'image_base64_2':img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc0f4cb5",
   "metadata": {},
   "source": [
    "## 2. 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "887b7af7",
   "metadata": {},
   "source": [
    "### 2.1人脸检测"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3979e1e5",
   "metadata": {},
   "source": [
    "### 2.1.1 人脸数据库的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "91718295",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '087935efb31362e6401675280efb7bd5', 'time_used': 178, 'face_count': 0, 'face_added': 0, 'request_id': '1679840541,299e9f44-88c2-4447-a99c-11efb2f20233', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'display_name':'广州南方学院1栋宿舍楼'\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd94f3c",
   "metadata": {},
   "source": [
    "### 2.1.2 检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7e15642",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'bf852f9f568f62c0e39fb4171d781c25', 'tags': '', 'time_used': 96, 'user_data': '', 'display_name': '广州南方学院1栋宿舍楼', 'face_tokens': [], 'face_count': 0, 'request_id': '1679840545,2ce69f36-2614-45ec-b131-4671a95ddc5b', 'outer_id': ''}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库集合详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'faceset_token': 'bf852f9f568f62c0e39fb4171d781c25',\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89bc3171",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fe9a7d58",
   "metadata": {},
   "source": [
    "### 2.1.3. 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "446c8953",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 81, 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id', 'request_id': '1679840751,c48d6a55-03ce-41eb-a4d9-29e32107ca4d'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：刘亦菲，蔡徐坤，高启强\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01 = open('lyf.jpg', 'rb')\n",
    "img = base64.b64encode(f_01.read())\n",
    "# 2. 酬载准备\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'image_base64_1':img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "51881f7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 60, 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id', 'request_id': '1679840773,f30893bb-7045-4530-9376-1fd8a5542ded'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：刘亦菲，蔡徐坤，高启强\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01 = open('cxk.jpg', 'rb')\n",
    "img = base64.b64encode(f_01.read())\n",
    "# 2. 酬载准备\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'image_base64_1':img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "18feb314",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 55, 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id', 'request_id': '1679840790,8f5280a6-0ae2-4a74-bc8c-5d662fde8f65'}\n",
      "400\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：刘亦菲，蔡徐坤，高启强\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_01 = open('gqq.jpg', 'rb')\n",
    "img = base64.b64encode(f_01.read())\n",
    "# 2. 酬载准备\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'image_base64_1':img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3ae462e5",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3051924677.py, line 13)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [6]\u001b[1;36m\u001b[0m\n\u001b[1;33m    'face_token':''\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params={\n",
    "    'api_key':'JM0sMbcI_nne7hJ5DtLi_gcNcCMKd-uU',\n",
    "    'api_secret':'4qUXiAJslJggSaEZsB2M2FdZq7aviKvh',\n",
    "    'faceset_token': 'bf852f9f568f62c0e39fb4171d781c25'\n",
    "    'face_token':''\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abdd6f53",
   "metadata": {},
   "source": [
    "### 人脸搜索（课后）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "202d65c9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
